package com.nazca.tvremote.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.nazca.tvremote.R;
import com.nazca.tvremote.event.RemoteKeyEvent;
import com.nazca.tvremote.service.Controller;

public class TVRemoteNum extends LinearLayout{

	private Controller mController;
	
	public void setController(Controller controller){
		mController = controller;
	}
	
	public TVRemoteNum(Context context) {
		super(context);
	}
	public TVRemoteNum(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	private View mContainer;
	private View mChVolView;
	private FrameLayout mNumView;
	private View mNavigationView;
	
	private Button mChVolToggleBtn;
	private View mChVolPad;
	
	private Button mNavigationToggleBtn;
	private View mNavigationPad;
	
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		
		mContainer = findViewById(R.id.tv_remote_num);
		mChVolView = mContainer.findViewById(R.id.ch_vol);
		mNavigationView = mContainer.findViewById(R.id.navigation);
		mNumView = (FrameLayout) mContainer.findViewById(R.id.num);
		
		mChVolToggleBtn = (Button) mChVolView.findViewById(R.id.toggle);
		mNavigationToggleBtn = (Button) mNavigationView.findViewById(R.id.toggle);
		
		mChVolPad = mChVolView.findViewById(R.id.pad);
		mNavigationPad = mNavigationView.findViewById(R.id.pad);
		
		initNumPad();
		
		initChVol();
		initNavigation();
	}
	
	private void initChVol(){
		mChVolToggleBtn.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				setChVolShow(!isChVolShown());
				setNavigationShow(false);
			}
		});
	}
	
	private void initNavigation(){
		mNavigationToggleBtn.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				setNavigationShow(!isNavigationShown());
				setChVolShow(false);
			}
		});
	}
	
	private void initNum(){
		mNumView.findViewById(R.id.num_1).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_2).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_3).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_4).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_5).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_6).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_7).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_8).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_9).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_0).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_minus).setOnClickListener(mNumOnClickListener);
		mNumView.findViewById(R.id.num_pre).setOnClickListener(mNumOnClickListener);
	}
	
	private boolean isChVolShown(){
		return mChVolPad.getVisibility() == View.VISIBLE;
	}
	
	private boolean isNavigationShown(){
		return mNavigationPad.getVisibility() == View.VISIBLE;
	}
	
	private void setChVolShow(boolean show){
		mChVolPad.setVisibility(show ? View.VISIBLE : View.GONE);
		mChVolToggleBtn.setBackgroundResource(show ? R.drawable.btn_ch_vol_hide : R.drawable.btn_ch_vol_show);
		mChVolToggleBtn.setText(show ? null : getContext().getString(R.string.tv_remote_ch_vol));
		initNumPad();
	}
	
	private void setNavigationShow(boolean show){
		mNavigationPad.setVisibility(show ? View.VISIBLE : View.GONE);
		mNavigationView.findViewById(R.id.a).setVisibility(show ? View.VISIBLE : View.GONE);
		mNavigationView.findViewById(R.id.b).setVisibility(show ? View.VISIBLE : View.GONE);
		mNavigationView.findViewById(R.id.c).setVisibility(show ? View.VISIBLE : View.GONE);
		mNavigationView.findViewById(R.id.d).setVisibility(show ? View.VISIBLE : View.GONE);
		mNavigationToggleBtn.setText(show ? null : getContext().getString(R.string.tv_remote_navigation));
		mNavigationToggleBtn.setBackgroundResource(show ? R.drawable.btn_navigation_hide : R.drawable.btn_navigation_show);
		initNumPad();
	}
	
	private void initNumPad(){
		int layoutRes = ( !isChVolShown() && !isNavigationShown() ) 
				? R.layout.view_num_large : R.layout.view_num_small;
		mNumView.removeAllViews();
		mNumView = (FrameLayout) LayoutInflater.from(getContext()).inflate(layoutRes, mNumView, true);
		
		initNum();
	}
	
	private View.OnClickListener mNumOnClickListener = new View.OnClickListener(){
		
		@Override
		public void onClick(View v) {
			int code = -1;
			switch (v.getId()) {
				case R.id.num_1:
					code = KeyEvent.KEYCODE_1;
					break;
				case R.id.num_2:
					code = KeyEvent.KEYCODE_2;
					break;
				case R.id.num_3:
					code = KeyEvent.KEYCODE_3;
					break;
				case R.id.num_4:
					code = KeyEvent.KEYCODE_4;
					break;
				case R.id.num_5:
					code = KeyEvent.KEYCODE_5;
					break;
				case R.id.num_6:
					code = KeyEvent.KEYCODE_6;
					break;
				case R.id.num_7:
					code = KeyEvent.KEYCODE_7;
					break;
				case R.id.num_8:
					code = KeyEvent.KEYCODE_8;
					break;
				case R.id.num_9:
					code = KeyEvent.KEYCODE_9;
					break;
				case R.id.num_0:
					code = KeyEvent.KEYCODE_0;
					break;
				case R.id.num_minus:
					code = KeyEvent.KEYCODE_0;
					break;
				case R.id.num_pre:
					code = KeyEvent.KEYCODE_0;
					break;
	
				default:
					break;
			}
			RemoteKeyEvent keyDownEvent = new RemoteKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, code));
			mController.println(keyDownEvent.toString());
			RemoteKeyEvent keyUpEvent = new RemoteKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, code));
			mController.println(keyUpEvent.toString());
		}
	};
}
